﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace BToolkit
{
    public class TouchScaleByTwoFingers : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
        public bool testInEditor;

        RectTransform trans;
        Vector2 canvasResolution;
        public float speed = 1f;
        public float min = 0.2f;
        public float max = 3f;
        int testDownNum;
        float previousDis;
        int fingerId1 = -1, fingerId2 = -1;
        Vector3 firstPoint, secondPoint;

        void Awake()
        {
            trans = transform as RectTransform;
            CanvasScaler canvasScaler = FindObjectOfType<CanvasScaler>();
            if (canvasScaler.matchWidthOrHeight > 0.5f)
            {
                canvasResolution = new Vector2(canvasScaler.referenceResolution.y * (float)Screen.width / (float)Screen.height, canvasScaler.referenceResolution.y);
            }
            else
            {
                canvasResolution = new Vector2(canvasScaler.referenceResolution.x, canvasScaler.referenceResolution.x * (float)Screen.height / (float)Screen.width);
            }
        }

        void Update()
        {
            if (testInEditor && Application.isEditor)
            {

            }
            else
            {
                if (fingerId1 != -1 && fingerId2 != -1)
                {
                    Touch touch1 = new Touch(), touch2 = new Touch();
                    for (int i = 0; i < Input.touchCount; i++)
                    {
                        Touch touch = Input.GetTouch(i);
                        if (touch.fingerId == fingerId1)
                        {
                            touch1 = touch;
                        }
                        else if (touch.fingerId == fingerId2)
                        {
                            touch2 = touch;
                        }
                    }
                    TwoPointUpdate(touch1.position, touch2.position);
                }
            }
        }

        public virtual void OnPointerDown(PointerEventData eventData)
        {
            if (fingerId1 == -1)
            {
                fingerId1 = eventData.pointerId;
            }
            else if (fingerId2 == -1)
            {
                previousDis = 0f;
                fingerId2 = eventData.pointerId;
            }
        }

        public virtual void OnPointerUp(PointerEventData eventData)
        {
            if (fingerId1 == eventData.pointerId)
            {
                fingerId1 = -1;
            }
            else if (fingerId2 == eventData.pointerId)
            {
                fingerId2 = -1;
            }
        }

        void TwoPointUpdate(Vector3 firstPos, Vector3 secondPos)
        {
            Vector3 fromPos = new Vector3(firstPos.x / (float)Screen.width, firstPos.y / (float)Screen.height, 0);
            Vector3 toPos = new Vector3(secondPos.x / (float)Screen.width, secondPos.y / (float)Screen.height, 0);
            if (previousDis == 0f)
            {
                previousDis = (new Vector2(toPos.x, toPos.y) - new Vector2(fromPos.x, fromPos.y)).magnitude;
            }
            else
            {
                float currDis = (new Vector2(toPos.x, toPos.y) - new Vector2(fromPos.x, fromPos.y)).magnitude;
                float deltaScale = (currDis - previousDis) * 5f * speed;
                previousDis = currDis;
                transform.localScale += new Vector3(deltaScale, deltaScale, deltaScale);
                if (transform.localScale.x < min)
                {
                    transform.localScale = Vector3.one * min;
                }
                else if (transform.localScale.x > max)
                {
                    transform.localScale = Vector3.one * max;
                }
            }
        }
    }
}